// pages/settings/settings.js
const app = getApp();

Page({
  data: {
    userInfo: {},
    deviceConnected: false
  },

  onLoad: function (options) {
    // 获取用户信息
    if (app.globalData.hasUserInfo && app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      });
    } else {
      // 从本地存储获取
      const userInfo = wx.getStorageSync('userInfo');
      if (userInfo) {
        this.setData({
          userInfo: userInfo
        });
      }
    }
  },

  // 退出登录
  logout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: function(res) {
        if (res.confirm) {
          // 清除本地存储的用户信息和token
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('token');
          
          // 清除全局数据
          app.globalData.userInfo = {};
          app.globalData.hasUserInfo = false;
          
          // 跳转到首页
          wx.switchTab({
            url: '../index/index'
          });
        }
      }
    });
  },
  // 返回上一页
  navigateBack: function() {
    wx.navigateBack();
  },
});